package com.yuzhenxin.coursesystem.controller.selection;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.framework.common.pojo.R;
import com.yuzhenxin.coursesystem.config.EndPoints;
import com.yuzhenxin.coursesystem.controller.selection.vo.SelectionQueryReqVO;
import com.yuzhenxin.coursesystem.entity.SelectionDO;
import com.yuzhenxin.coursesystem.service.SelectionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "选课管理")
@RestController
public class SelectionController {

    @Resource
    private SelectionService selectionService;

    @GetMapping(EndPoints.SELECTION_GETBYID)
    public R<SelectionDO> getById(@RequestParam("id") Integer id) {
        SelectionDO selectionDO = selectionService.getById(id);
        return R.success(selectionDO);
    }

    @PostMapping(value = EndPoints.SELECTION_SELECTPAGE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<IPage<SelectionDO>> selectPage(SelectionQueryReqVO reqDto) {
        IPage<SelectionDO> selectionIPage = selectionService.selectPage(reqDto);
        return R.success(selectionIPage);
    }

    @Operation(summary = " 每门课程多少学生修")
    @GetMapping(value = EndPoints.SELECTION_SELECTSTUDENTCOUNT)
    public R<List<Map<String, Object>>> selectStudentCount(@RequestParam("current") int current, @RequestParam("limit") int limit) {
        List<Map<String, Object>> result = selectionService.selectStudentCount(current, limit);
        return R.success(result);
    }

    @Operation(summary = "每个学生修了多少门课")
    @GetMapping(EndPoints.SELECTION_SELECTCOURSECOUNT)
    public R<List<Map<String, Object>>> selectCourseCount(@RequestParam("current") int current, @RequestParam("limit") int limit) {
        List<Map<String, Object>> result = selectionService.selectCourseCount(current, limit);
        return R.success(result);
    }

    @Operation(summary = "根据学号查看我的选课信息")
    @GetMapping(EndPoints.SELECTION_SELECTBYSTUDENTNUMBER)
    public R<List<Map<String, Object>>> selectByStudentNumber(@RequestParam("studentNumber") Integer studentNumber) {
        List<Map<String, Object>> result = selectionService.selectByStudentNumber(studentNumber);
        return R.success(result);
    }

    @Operation(summary = "查看所有选课信息")
    @GetMapping(EndPoints.SELECTION_SELECTLIST)
    public R<List<Map<String, Object>>> selectList() {
        List<Map<String, Object>> selection = selectionService.selectList();
        return R.success(selection);
    }

    @PostMapping(value = EndPoints.SELECTION_SELECTCOURSE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<?> selectCourse(@RequestParam("studentId") Long studentId, @RequestParam("scheduleId") Long scheduleId) {
        selectionService.selectCourse(studentId, scheduleId);
        return R.success();
    }

    @Operation(summary = "修改选课信息的成绩")
    @PutMapping(value = EndPoints.SELECTION_UPDATESCORE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<?> updateScore(@RequestBody SelectionDO selectionDO) {
        selectionService.updateById(selectionDO);
        return R.success();
    }

    @Operation(summary = "删除多个选课信息")
    @DeleteMapping(value = EndPoints.SELECTION_REMOVEBYIDS, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<?> removeByIds(@RequestBody List<Integer> selectionIdList) {
        selectionService.remove(selectionIdList);
        return R.success();
    }

}
